package cn.mark.test.serlvet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(urlPatterns="/uploadFile/*",asyncSupported=false)
@MultipartConfig(location="/home/mark/temp/file" )
public class UploadServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		resp.getWriter().println("Upload start");
		super.service(req, resp);
//		
//		System.out.println("getContextPath" + req.getContextPath());
//		System.out.println("getServletPath" + req.getServletPath());
//		System.out.println("getPathInfo" + req.getPathInfo());
//		
//		HttpSession session = req.getSession();
//		
//		System.out.println("Session ID: " + session.getId());
//		
//		
//		resp.getWriter().println("req.getRequestURI(): " + req.getRequestURI());
//		resp.getWriter().println("resp.encodeURL(req.getRequestURI()): " + resp.encodeURL(req.getRequestURI()));
////		resp.encodeURL(req.getRequestURI()); // 会将sessionId保持在URL,如果client设置cookie允许，则保持不变
//		
//		
//	
//		
//		
//		resp.getWriter().println("World2");
//		
////		req.getRequestDispatcher("/report2").include(req, resp);
////		req.getRequestDispatcher("/report2").forward(req, resp);
//		
//		resp.getWriter().println("getRequestDispatcher end");
		
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		System.out.println("req.getInputStream().available(): " + req.getInputStream().available());
//		System.out.println("upload isReady: 2" + req.getInputStream().isReady());
//		byte buffer[] = new byte[4*1024];
//		int length = req.getInputStream().read(buffer);
//		StringBuilder sbuilder = new StringBuilder();
//		sbuilder.append(new String(buffer, 0, length));
//		System.out.println(sbuilder.toString());
		// TODO Auto-generated method stub
//		super.doPost(req, resp);
		Part part = req.getPart("userPhone");
		
		System.out.println("" + part.getName());
		System.out.println("" + part.getSubmittedFileName());
		System.out.println("===getHeaderNames start====");
		part.getHeaderNames().forEach(s ->{System.out.println(s);});
		System.out.println("===getHeaderNames end====");
		
		part.write("/home/mark/temp/fww.txt");
		
		System.out.println("END");
	}
	
	
	
}
